<?php
	include_once "db/db.php";

	if ($_REQUEST['type'] == "money") {
?>
	<div id="dialog-money" class="dialog" title="Gain / lose Money">
	<form action="play-actions.php" method="post">
		<table>
			<tr>
				<td colspan="2">
					<input type="radio" name="moneyAction" value="payday"
							id="moneyActionPayDay" checked /><label for="moneyActionPayDay">Pay day</label>
					<input type="radio" name="moneyAction" value="taxes"
							id="moneyActionTaxes"/><label for="moneyActionTaxes">Taxes due</label>
					<input type="radio" name="moneyAction" value="earn" 
							id="moneyActionEarn"/><label for="moneyActionEarn">Earn...</label>
					<input type="radio" name="moneyAction" value="pay" 
							id="moneyActionPay"/><label for="moneyActionPay">Pay...</label>
				</td>
			</tr>
			<tr id="moneyRowAmount">
				<th>Amount</th>
				<td><input name="amount" class="money" id="moneyInputAmount"/></td>
			</tr>
			<tr id="moneyRowEvent">
				<th>Event</th>
				<td><input name="event"/></td>
			</tr>
			<tr id="moneyRowCareer">
				<th>Type</th>
				<td>
					<select name="tocareer">
						<option value="">None</option>
					<?php
						$career_query = mysql_query("SELECT * FROM `career` ORDER BY `id`");
						while ($career = mysql_fetch_assoc($career_query)) {
							echo "<option value=\"" . $career['id'] . "\">" . $career['name'] . "</option>";
						}
					?>
					</select>
				</td>
			</tr>
			<tr>
				<td class="actions" colspan="2">
					<input type="submit" name="moneySubmit" value="Save"/>
					<input type="reset" value="Cancel"/>
				</td>
			</tr>
		</table>
	</form>
	</div>

<?php
	} else if ($_REQUEST['type'] == "career-income") {
?>

	<div id="dialog-career-income" class="dialog" title="Select your career / income">
	<form action="play-actions.php" method="post">
	<?php
		echo cards_html("career", "
				SELECT `career`.* 
				FROM `career` LEFT JOIN `gameplayer` ON (`career`.`id` = `gameplayer`.`career` AND `gameplayer`.`game` = '$current_game_id') 
				WHERE `gameplayer`.`player` IS NULL
				ORDER BY RAND()");
		
		echo cards_html("salary", "
				SELECT `income`.* 
				FROM `income` LEFT JOIN `gameplayer` ON (`income`.`id` = `gameplayer`.`income` AND `gameplayer`.`game` = '$current_game_id')
				WHERE `gameplayer`.`player` IS NULL
				ORDER BY RAND()");
	?>
		<input type="reset" value="Cancel"/>
	</form>
	</div>

<?php
	} else if ($_REQUEST['type'] == "family") {
?>

	<div id="dialog-family" class="dialog" title="Add to your Family">
	<form action="play-actions.php" method="post">
		<table>
			<tr>
				<th>Do what?</th>
				<td>
					<select name="type">
						<option value="marry">Get married</option>
						<option value="girl">Give birth to / adopt baby girl</option>
						<option value="boy">Give birth to / adopt baby boy</option>
					</select>
				</td>
			</tr>
			<tr>
				<td colspan="2">
					<input type="submit" value="Save" name="familySubmit"/>
					<input type="reset" value="Cancel"/>
				</td>
			</tr>
		</table>
	</form>
	</div>

<?php
	} elseif ($_REQUEST['type'] == "house") {
?>

	<div id="dialog-house" class="dialog" title="Choose a house...">
	<form action="play-actions.php" method="post">
	<?php
		echo cards_html("house", "
				SELECT `house`.* 
				FROM `house` 
					LEFT JOIN `gameplayer` ON (`house`.`id` = `gameplayer`.`house` AND `gameplayer`.`game` = '$current_game_id')
				WHERE `gameplayer`.`player` IS NULL
				ORDER BY RAND()");
	?>
		<input type="reset" value="Cancel"/>
	</form>
	</div>

<?php
	} elseif ($_REQUEST['type'] == "lifecard") {
?>

	<div id="dialog-lifecard" class="dialog" title="Choose a life card...">
	<form action="play-actions.php" method="post">
	<?php
		$lifecards_query = "
				SELECT `lifecard`.* 
				FROM `lifecard` 
					LEFT JOIN `playerlifecard` ON (`lifecard`.`id` = `playerlifecard`.`lifecard` AND `playerlifecard`.`game` = '$current_game_id')
				WHERE `playerlifecard`.`player` IS NULL AND NOT " . 
					get_millionaire_lifecards_query_condition($current_game_id) . "
				ORDER BY RAND()";
		if (mysql_num_rows(mysql_query($lifecards_query)) == 0) {
			// If the lifecards have run out, allow stealing from another player
			$players_query = mysql_query("
					SELECT `player`.`id`, `player`.`name`, `retirementhome`.`name` AS `retirementhome`
					FROM `gameplayer` 
						LEFT JOIN `player` ON (`gameplayer`.`player` = `player`.`id`)
						LEFT JOIN `retirementhome` ON (`gameplayer`.`retired` = `retirementhome`.`id`)
					WHERE 
						(`retirementhome`.`lifecardssafe` = '0' OR `retirementhome`.`id` IS NULL) AND
						`gameplayer`.`game` = '$current_game_id' AND
						`player`.`id` != '" . $current_game['turn'] . "'
					GROUP BY `gameplayer`.`player`
					ORDER BY `player`.`name`");
			while ($player = mysql_fetch_assoc($players_query)) {
				$player_lifecards_query = "
						SELECT `lifecard`.* 
						FROM `playerlifecard` 
							LEFT JOIN `lifecard` ON (`playerlifecard`.`lifecard` = `lifecard`.`id`)
						WHERE `playerlifecard`.`game` = '$current_game_id' AND
							`playerlifecard`.`player` = '" . $player['id'] . "'
						ORDER BY RAND()";
				echo "Steal from " . $player['name'] . ":" . cards_html("life", $player_lifecards_query, $player['id']);
			}
		} else {
			// There are still lifecards left in the deck, take from there
			echo cards_html("life", $lifecards_query);
		}
	?>
		<input type="reset" value="Cancel"/>
	</form>
	</div>

<?php
	} elseif ($_REQUEST['type'] == "retire") {
?>

	<div id="dialog-retire" class="dialog" title="Retire from your job to a retirement home">
	<form action="play-actions.php" method="post">
		<table>
			<tr>
			<?php
				$retirementhome_query = mysql_query("SELECT * FROM `retirementhome` ORDER BY `name`");
				while ($retirementhome = mysql_fetch_assoc($retirementhome_query)) {
					$id = $retirementhome['id'];
					echo "
						<tr>
							<td>
								<input type=\"radio\" name=\"retirementHome\" value=\"$id\" id=\"retirementHome$id\"/>
							</td>
							<td>
								<label for=\"retirementHome$id\">
									<img src=\"images/retirement/" . 
										strtolower(str_replace(" ", "-", $retirementhome['name'])) . 
										".jpg\" alt=\"" . $retirementhome['name'] . "\"
										style=\"width: 200px; height:auto;\"/>
								</label>
							</td>
							<td>
								" . ($retirementhome['lifecardssafe'] == 1 ? 
										"Your life cards are safe" : "Your life cards are vulnerable") . "
								<br/>
								" . ($retirementhome['lifecards'] > 0 ? "There are " . $retirementhome['lifecards'] . " life cards up for grabs!" : "") . "
							</td>
						</tr>";
				}
			?>
			</tr>
			<tr>
				<td colspan="3" class="actions">
					<input type="submit" name="retireSubmit" value="Retire"/>
					<input type="reset" value="Cancel"/>
				</td>
			</tr>
		</table>
	</form>
	</div>

<?php
	} elseif ($_REQUEST['type'] == "insurance") {
?>

	<div id="dialog-insurance" class="dialog" title="Get insurance">
	<form action="play-actions.php" method="post">
		<table>
			<tr>
				<td>
					<input type="radio" name="insuranceType" value="car" id="insuranceTypeCar"/>
				</td>
				<td>
					<label for="insuranceTypeCar">
						<img src="images/insurance_debt/car-insurance.jpg" style="width:200px; height:auto;"/>
					</label>
				</td>
			</tr>
			<tr>
				<td>
					<input type="radio" name="insuranceType" value="house" id="insuranceTypeHouse"/>
				</td>
				<td>
					<label for="insuranceTypeHouse">
						<img src="images/insurance_debt/home-owners-insurance.jpg" style="width:200px; height:auto;"/>
					</label>
				</td>
			</tr>
			<tr>
				<td colspan="2" class="actions">
					<input type="submit" name="insuranceSubmit" value="Save"/>
					<input type="reset" value="Cancel"/>
				</td>
			</tr>
		</table>
	</form>
	</div>

<?php
	} else if ($_REQUEST['type'] == "stocks") {
?>

	<div id="dialog-stocks" class="dialog" title="Choose your stock to invest in...">
	<form action="play-actions.php" method="post">
	<?php
		echo cards_html("stock", "SELECT * FROM `stock` ORDER BY RAND()");
	?>
		<input type="reset" value="Cancel"/>
	</form>
	</div>
	
<?php
	}
?>
